package view.macrogui
{
	import events.GuiEvent;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	/**
	 */
	public class TabSelectorButton extends Sprite
	{
		private const FORMAT:TextFormat = new TextFormat("Helvetica", 11, 0xFFFFFF);
		static private const BG_NORMAL:Number = 0x555555;
		static private const BG_HOVER:Number = 0x666666;
		static private const BG_CLICK:Number = 0x777777;
		private var text_field:TextField;
		private var w:int;
		
		public function TabSelectorButton(text:String, w:int)
		{
			this.w = w;
			
			bg(BG_NORMAL);
			
			text_field = new TextField();
			text_field.selectable = false;
			text_field.defaultTextFormat = FORMAT;
			addChild(text_field);
			
			this.text = text;
			
			addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void
				{
					dispatchEvent(new GuiEvent(GuiEvent.CHANGE_TAB, text));
				});
			addEventListener(MouseEvent.MOUSE_OVER, function(e:Event):void
				{
					bg(BG_HOVER);
				});
			addEventListener(MouseEvent.MOUSE_OUT, function(e:Event):void
				{
					bg(BG_NORMAL);
				});
			addEventListener(MouseEvent.MOUSE_DOWN, function(e:Event):void
				{
					bg(BG_CLICK);
				});
			addEventListener(MouseEvent.MOUSE_UP, function(e:Event):void
				{
					bg(BG_HOVER);
				});
		}
		
		public function bg(color:int):void
		{
			graphics.clear();
			graphics.beginFill(color);
			graphics.drawRoundRectComplex(0, 0, w, TabSelector.HEIGHT, 8, 8, 0, 0);
			graphics.endFill();
		}
		
		public function get text():String
		{
			return text_field.text;
		}
		
		public function set text(value:String):void
		{
			text_field.text = value;
			text_field.width = text_field.textWidth + 6;
			text_field.height = text_field.textHeight + 3;
			text_field.x = int((w - (text_field.textWidth + 3)) / 2);
		}
	
	}

}